﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using CognisessService;
using CognisessDA;
using CognisessCommLib;
using Webdiyer.WebControls.Mvc;
using CognisessWeb.Models;

namespace CognisessWeb.Controllers
{
    public class UserMasterController : Controller
    {
        AccountBLL accountBLL = new AccountBLL();

        #region Action Methods

        #region Get

        //  GET: /UserMaster/
        public ActionResult Index(int? id)
        {
            PagedList<User> result = accountBLL.GetPagingUser(new User()).ToPagedList(id ?? 1, 10);
            if (Request.IsAjaxRequest())
                return PartialView("UserListOfView", result);
            return View(result);
        }

        // GET: /UserMaster/Details/
        public ActionResult Details(int id)
        {
            User result = accountBLL.GetUser(new User { Id = id }).FirstOrDefault();
            this.SetDDLDataSourceAndDefaultSelectValue(result.EducationLevel, result.TimeZone, result.HowToKnowUs);
            return View(result);
        }

        // GET: /UserMaster/Create
        public ActionResult Create()
        {
            this.SetDDLDataSourceAndDefaultSelectValue(0, 0, 0);
            return View();
        }

        // GET: /UserMaster/Edit/5
        public ActionResult Edit(int id)
        {
            User result = accountBLL.GetUser(new User { Id = id }).FirstOrDefault();
            this.SetDDLDataSourceAndDefaultSelectValue(result.EducationLevel, result.TimeZone, result.HowToKnowUs);
            return View(result);
        }

        // GET: /UserMaster/Delete/5
        public ActionResult Delete(int id)
        {
            bool isSuccessful = accountBLL.DeleteUser(id);
            if (isSuccessful)
            {
                TempData["ExcuteSuccfull"] = "Delete Successfully.";
            }
            else
            {
                TempData["ExcuteSuccfull"] = "Operation fail. Please contact with the administrator.";
            }
            return RedirectToAction("Index");
        }

        

        



        

        

        

        

        #endregion

        #region Post

        // POST: /UserMaster/Create
        [HttpPost]
        public ActionResult Create(User collection)
        {
            try
            {
                if (accountBLL.GetUser(new User { Username = collection.Username }).Count > 0)
                {
                    ViewData.ModelState.AddModelError("Username", ConstantHelper.ERROR_USER_EXIST);
                }
                if (accountBLL.GetUser(new User { Email = collection.Email }).Count > 0)
                {
                    ViewData.ModelState.AddModelError("Email", ConstantHelper.ERROR_EMAIL_EXIST);
                }
                if (ViewData.ModelState.IsValid)
                {
                    accountBLL.AddUser(new List<User> { collection });
                    return RedirectToAction("Index");
                }
                this.SetDDLDataSourceAndDefaultSelectValue(collection.EducationLevel, collection.TimeZone, collection.HowToKnowUs);
                return View(collection);

            }
            catch
            {
                return View();
            }

        }

        // POST: /UserMaster/Edit/5
        [HttpPost]
        public ActionResult Edit(int id, User collection)
        {
            try
            {
                if (accountBLL.GetUser(new User { Username = collection.Username }).Count > 0)
                {
                    ViewData.ModelState.AddModelError("Username", ConstantHelper.ERROR_USER_EXIST);
                }
                if (accountBLL.GetUser(new User { Email = collection.Email }).Count > 0)
                {
                    ViewData.ModelState.AddModelError("Email", ConstantHelper.ERROR_EMAIL_EXIST);
                }
                if (ViewData.ModelState.IsValid)
                {
                    accountBLL.UpdataUser(new List<User> { collection });
                    return RedirectToAction("Index");
                }
                this.SetDDLDataSourceAndDefaultSelectValue(collection.EducationLevel, collection.TimeZone, collection.HowToKnowUs);
                return View(collection);
            }
            catch
            {
                return View();
            }
        }

        // POST: /UserMaster/Delete/5
        [HttpPost]
        public ActionResult Delete(int id, FormCollection collection)
        {
            try
            {
                // TODO: Add delete logic here

                return RedirectToAction("Index");
            }
            catch
            {
                return View();
            }
        }

        

        




        
        

        #endregion
        #endregion

        #region Private Methods
        private void SetDDLDataSourceAndDefaultSelectValue(int educationSelectValue, int timeZoneTSelectValue, int aboutSelectValue)
        {
            ViewData["Education"] = new SelectList(XMLHelper.GetOptions("Education"), "Value", "Text", educationSelectValue);
            ViewData["TimeZoneT"] = new SelectList(XMLHelper.GetOptions("TimeZone"), "Value", "Text", timeZoneTSelectValue);
            ViewData["About"] = new SelectList(XMLHelper.GetOptions("About"), "Value", "Text", aboutSelectValue);
        }
        #endregion





    }
}
